/**
 * 每天任务
 */
var loadTasks = function(){
	var date = $('#date').val();
	var userId = $('#userId').val();
	var url = 'wx/task/load/2/'+userId;
	
	$.ajax({
        url: url,
        type: "get",
        data: {
        	start : date
        },
        success: function(data) {
        	data = eval(data);
        	var html = '';
        	
        	for(var i = 0; i < data.length; i++){
				var item = data[i];
				var type = '<span class="am-badge am-badge-warning">领导分配</span>';
				if(item.type == 1){
					type = '<span class="am-badge am-badge-primary">项目任务</span>';
				}else if(item.type == 2){
					type = '<span class="am-badge am-badge-secondary">日常工作</span>';
				}else if(item.type == 3){
					type = '<span class="am-badge am-badge-success">个人计划</span>';
				}
				
				html += '<li class="am-form am-form-horizontal"><div>';
				html +='<span class="am-badge am-round">'+(i+1)+'</span>&nbsp;'+type+'&nbsp;';
				if(item.state == 2){
					html +='<span class="am-badge am-badge-danger">已延期</span>';
				}else if(item.state == 3){
					html +='<span class="am-badge am-badge-danger">按时完成</span>';
				}else if(item.state == 4){
					html +='<span class="am-badge am-badge-danger">延期完成</span>';
				}
				html+= '<br>';
				html +='计划：'+item.content+'<br>';
				if(item.result){
					html +='完成情况：'+item.result+'<br>';
				}
				if(item.finishTime){
					html +='完成时间：'+item.finishTime+'<br>';
				}
				if(item.duration){
					html +='使用时长：'+item.duration+'分钟<br>';
				}
				
				if(item.type != 2){
					html +='计划开始时间：'+item.start+'<br>';
					html +='计划结束时间：'+item.end+'<br>';
				}
				
				if(item.state != 3 && item.state != 4){
					html +='<div class="am-btn-group am-btn-group-sm" >';
				
					if(item.type != 2){
						html +='<button type="button" data-id="'+item.id+'" data-type=1 class="am-btn am-btn-default am-radius">直接完成</button>';
						html +='<button type="button" data-id="'+item.id+'" data-type=2 class="am-btn am-btn-default am-radius">明天再做</button>';
					}
					html +='<button type="button" data-id="'+item.id+'" data-type=3 class="am-btn am-btn-default am-radius">处理</button>';
					html +='<button type="button" data-id="'+item.id+'" data-type=4 class="am-btn am-btn-default am-radius">取消</button>';
					html +='</div>';
				}
				
				html +='</div></li>';
			};
			
			if(html == ''){
				html = '你今天比较空闲哦...';
			}

			$("#list").html(html);
			
			$('.am-btn-group').find('.am-btn').on('click', function() {
				if($(this).data('type') == 3){
					$('#my-actions').modal({
						relatedTarget: this,
				        onConfirm: function(options) {
				        	var $btn =$(this.relatedTarget);
				        	if($('#result').val() != '' || $('#end').val() != ''){
				        		executeTask($btn.data('type'),$btn.data('id'),{
					            	result : $('#result').val(),
					            	duration : $('#duration').val(),
					            	end : $('#end').val()
					            });
					            $('#my-actions').modal('close');
				        	}else{
				        		$('#err-alert').show();
				        		setTimeout(function(){
				        			$('#err-alert').hide();
				        	    },3000);
				        	}
				            
				        }
					});
				}else{
					$('#confirm').modal({
				        relatedTarget: this,
				        onConfirm: function(options) {
				        	var $btn =$(this.relatedTarget);
				            executeTask($btn.data('type'),$btn.data('id'));
				        },
				        onCancel: function() {
				          
				        }
				   });
				}
				
			      
		    });
        }
    });
};

var executeTask = function(type,id,data){
	var userId = $('#userId').val();
	var url = 'wx/task/execute_' + type + '/' + userId + '/' + id;
	$.ajax({
		url : url,
		data : data,
		type : "post",
		success : function(data) {
			if(data == '操作成功'){
				loadTasks();
			}else{
				alert(data);
			}
			
		}
	});
};

$(function() {
	$('#end').datetimepicker({
		lang:'ch',
		format:'Y-m-d H:i',
		formatDate:'Ymd',
		formatTime: 'H:i'//格式化时间
	});
	$('#err-alert').hide();
	loadTasks();
	
});

